package com.yufu.navigation.bar;

import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.RequiresApi;

import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
import com.getcapacitor.util.WebColor;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Locale;

@CapacitorPlugin(name = "NavigationBar")
public class NavigationBarPlugin extends Plugin {

    private NavigationBar implementation;
    private Window window;

    @Override
    public void load() {
        super.load();
        window = getActivity().getWindow();
        this.implementation = new NavigationBar(getActivity());
    }

    @PluginMethod
    public void getStatusBarHeight(PluginCall call) {
        Resources resources = getContext().getResources();
        int identifier = resources.getIdentifier("status_bar_height", "dimen", "android");
        int height = 0;
        if (identifier > 0) {
            height = resources.getDimensionPixelSize(identifier);
        } else {
            float density = resources.getDisplayMetrics().density;
            double dh;
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
                dh = Math.ceil(24 * density);
            } else {
                dh = Math.ceil(25 * density);
            }
            height = (int) dh;
        }
        float density = resources.getDisplayMetrics().density;
        call.resolve(new JSObject().put("height", (int) (height / density + 0.5)));
    }

    @PluginMethod
    public void setColor(PluginCall call) {
        String color = call.getString("color");
        if (color == null) {
            call.reject("color must be provided");
            return;
        }

        getBridge().executeOnMainThread(
                () -> {
                    try {
                        final int parsedColor = WebColor.parseColor(color.toUpperCase(Locale.ROOT));
                        window.setNavigationBarColor(parsedColor);
                        call.resolve();
                    } catch (IllegalArgumentException ex) {
                        call.reject("Invalid color provided. Must be a hex string (ex: #ff0000");
                    }
                });
    }

    @PluginMethod
    public void setDividerColor(PluginCall call) {
        String color = call.getString("color");
        if (color == null) {
            call.reject("color must be provided");
            return;
        }

        int colorInt = Color.parseColor(color);
        getBridge().executeOnMainThread(
                () -> {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                        window.setNavigationBarDividerColor(colorInt);
                    }
                    call.resolve();
                });
    }

    @PluginMethod
    public void hide(final PluginCall call) {
        getBridge().executeOnMainThread(
                () -> {
                    implementation.hide();
                    call.resolve();
                });
    }

    @PluginMethod
    public void show(final PluginCall call) {
        getBridge().executeOnMainThread(
                () -> {
                    implementation.show();
                    call.resolve();
                });
    }

    @PluginMethod
    public void getInfo(final PluginCall call) {
        NavigationBarInfo info = implementation.getInfo();
        JSObject data = new JSObject();
        data.put("visible", info.isVisible());
        data.put("divider_color", info.getDividerColor());
        data.put("color", info.getColor());
        data.put("overlays", info.isOverlays());
        call.resolve(data);
    }

    @PluginMethod
    public void setOverlaysWebView(final PluginCall call) {
        final Boolean overlays = call.getBoolean("overlay", true);
        getBridge().executeOnMainThread(
                () -> {
                    assert overlays != null;
                    implementation.setOverlaysWebView(overlays);
                    call.resolve();
                });
    }
}
